<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的购物车</title>
    <link href="../asserts/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    <script src="../asserts/plugins/jquery-3.6.0.min.js"></script>
    <script src="../asserts/plugins/bootstrap/js/bootstrap.js"></script>

    <script>
        $(function () {
            function init() {
                $.ajax({
                    url: "/KunKunMarket/CartController",
                    type: "get",
                    dataType: "json",
                    data: {
                        type: "getListByUser_id",
                    },
                    success: function (respBean) {
                        let $commodityList = $("#commodityList").text("");
                        if (respBean.status == 500){
                            $commodityList.html('<h1>未获取到任何信息</h1>')
                            return;
                        }
                        for (let i=0; i<respBean.data.length; i++){
                            console.log(respBean.data[i]);
                        }
                        //获取商品，添加到元素中
                        for (let i=0; i<respBean.data.length; i++){
                            //创建html标签元素
                            $commodity = $(`
                            <div class="col-lg-3 col-md-4 col-sm-6 col-xs-6">
                                <div class="thumbnail">
                                    <div class="caption">

                                        <h4 id="commodity_name"  >华为畅享7</h4>
                                        <p style="color:red;">
                                            <span>价格：</span> <span id="commodity_price" ></span>
                                        </p>
                                        <p id="commodity_stock" style="display: none"> </p>
                                        <p id="commodity_id" style="color:red;display: none"></p>
                                        <p id="commodity_user_id" style="color:red;display: none"></p>
                                        <p id="cart_id" style="color:red;display: none"></p>
                                        <button data-target="#tanChuang"  class="addOrder_btn btn btn-default" style="width: 75px" data-toggle="modal">购买</button>
                                        <button class="deleteCart_btn btn btn-danger btn-xs" style="float: right;margin-top: 7px">移出购物车</button>
                                    </div>
                                </div>
                            </div>
                            `);
                            //{ commodity_id: 20, id: 7, name: "神舟战神", price: 999, user_id: 6 }

                            $commodity.find("#commodity_name").text(respBean.data[i].name);
                            $commodity.find("#commodity_price").text(respBean.data[i].price);
                            $commodity.find("#commodity_stock").text(respBean.data[i].stock)

                            $commodity.find("#cart_id").text(respBean.data[i].id)
                            $commodity.find("#commodity_id").text(respBean.data[i].commodity_id)
                            $commodity.find("#commodity_user_id").text(respBean.data[i].user_id);

                            //将新创建的元素添加到商品列表容器
                            $commodityList.append($commodity);
                        }
                        // 给每个商品内的按钮添加点击事件
                        $(".addOrder_btn").click(addOrder_btn);
                        $(".deleteCart_btn").click(deleteCart_btn);

                    }
                })
            }
            init();

            //呼出购买商品的表单时，填充数据
            function addOrder_btn(){
                //获取到商品id, 设置到表单的异常域中
                let commodity_id = $(this).parents(".caption").find("#commodity_id").text();
                $("input[name=commodity_id]").val(commodity_id);
            }
            //购买商品，生成订单
            $("button[name=addOrder]").click(function () {
                $.ajax({
                    url:"/KunKunMarket/OrderController",
                    type:"get",
                    dataType:"json",
                    data: {
                        type:'addOrder',
                        commodity_id: $("input[name=commodity_id]").val(),
                        phone: $("input[name=phone]").val(),
                        address: $("input[name=address]").val(),
                        password: $("input[name=password]").val()
                    },
                    success:function (respBean) {
                        alert(respBean.msg);
                        if (respBean.status == 200){
                            $(".cel").click();
                        }
                    }
                })
            })
            //移出购物车
            function deleteCart_btn() {
                let id = $(this).parents(".caption").find("#cart_id").text();
                $.ajax({
                    url:"/KunKunMarket/CartController",
                    type:"get",
                    dataType:"json",
                    data: {
                        type:'deletetById',
                        id: id,
                    },
                    success:function (respBean) {
                        alert(respBean.msg);
                        if (respBean.status == 200){
                            $(".cel").click();
                            init();
                        }
                    }
                })
            }

        })
    </script>

</head>
<body>

    <div class="container">
        <a href="/KunKunMarket/UserController?type=logout">退出登录</a>
        <!-- 张略缩图开始 -->
        <div class="row" id="commodityList">

        </div>
    </div>

    <!--fade 弹出表单-->
    <div class="modal fade"  id="tanChuang" aria-labelledby="myModallabel" aria-hidden="true" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h5 class="modal-title" id="myModallabel">请填写收货信息</h5>
                </div>
                <form id="formData" class="form-horizontal">
                    <input type="hidden" name="commodity_id">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="control-label col-lg-2">密码：</label>
                            <div class="col-lg-9">
                                <input type="password" value="" name="password" class="form-control"></br>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-lg-2">电话：</label>
                            <div class="col-lg-9">
                                <input type="text" value="" name="phone" class="form-control"></br>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-lg-2">地址：</label>
                            <div class="col-lg-9">
                                <input type="text" value="" name="address" class="form-control">
                            </div>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button class="cel btn btn-default" type="button" data-dismiss="modal">取消</button>
                        <button class="btn btn-success" type="button" name="addOrder">确认</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

</body>
</html>